/*
 * @Descripttion: 饼图公共组件
 * @version: 1.0.0
 * @Author: chengengwei
 * @Date: 2020-12-01 18:16:21
 */
import echarts from 'echarts'
export const echartsPieMixin = {
  data() {
    return {
      chartInstance: {},
      pieData: {
        title: {}, // 标题
        legend: {}, // 图例
        series: [], // 数据
        grid: {}, // 间隔
        dataZoom: {} // 缩放组件
      }
    }
  },
  computed: {
    option() {
      return {
        title: this.pieData.title,
        color: this.pieData.color,
        legend: this.pieData.legend,
        series: this.pieData.series,
        grid: this.pieData.grid,
        dataZoom: this.pieData.dataZoom
      }
    }
  },
  methods: {
    drawChart(options, id) {
      if (document.getElementById(id)) {
        this.chartInstance = echarts.init(document.getElementById(id), 'light')
        // 添加自定义事件
        if (this.addChartEvent) {
          this.addChartEvent(this.chartInstance)
        }
        this.chartInstance.setOption(options)
        this.chartInstance.resize()
      }
    },
    /** 清空数据 */
    clearChart() {
      if (this.chartInstance.clear) {
        this.chartInstance.clear()
      }
    }
  }
}
